package com.tlcx.kfip.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.orhanobut.logger.Logger;
import com.tlcx.kfip.R;
import com.tlcx.kfip.adapter.base.BaseAdapter;
import com.tlcx.kfip.entity.IPRecordGoingEntity;
import com.tlcx.kfip.utils.AppInfoUtils;

/**
 * Created by wanjixin on 2016/10/14.
 */

public class OngoingAdapter extends BaseAdapter<IPRecordGoingEntity> implements View.OnClickListener {
    public OngoingAdapter(Context context) {
        super(context);
    }
    @Override
    public int getItemResource(int viewType) {
        return R.layout.adapter_ongoing;
    }

    @Override
    public View getItemView(int position, View convertView, ViewHolder viewHolder) {
        IPRecordGoingEntity item = getItem(position);
        if (item == null) {
            return convertView;
        }

        //设置标题
        TextView titleTv = viewHolder.getView(R.id.tv_ongoing_item_name);
        titleTv.setText(item.getTitle());

        //设置icon
        SimpleDraweeView iconIv = viewHolder.getView(R.id.iv_ongoing_item_icon);
        Uri uri = Uri.parse("res://" + AppInfoUtils.getCurrentPkgName(mContext) + "/" + item.getIconUrl());
        iconIv.setImageURI(uri);

        //设置进度
        ProgressBar progressBar = viewHolder.getView(R.id.pb_ongoing_item_progress);
        progressBar.setProgress(item.getProgress());

        //设置活动需求、及剩余名额情况
        TextView participateInfoTv = viewHolder.getView(R.id.tv_ongoing_item_price);
        participateInfoTv.setText(getParticipateInfoStyle(item.getNeedCounts(),item.getRemindCounts()));

        //设置活动参与情况
        TextView participateCountTv = viewHolder.getView(R.id.tv_ongoing_item_amount);
        participateCountTv.setText(getParticipateCountStyle(item.getParticipateCounts()));

        //设置追加的点击事件
        TextView addTv = viewHolder.getView(R.id.tv_ongoing_item_add);
        addTv.setTag(item);
        addTv.setOnClickListener(this);

        //设置查看夺宝号点击事件
        TextView checkIpIdTv = viewHolder.getView(R.id.tv_ongoing_item_look_number);
        checkIpIdTv.setTag(item);
        checkIpIdTv.setOnClickListener(this);

        return convertView;
    }

    /**
     * 设置总需人数和剩余人数
     * @param needCount
     * @param remindCount
     * @return
     */
    private Spanned getParticipateInfoStyle(int needCount,int remindCount){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_need_all));
        buffer.append(needCount);
        buffer.append(mContext.getString(R.string.ip_records_tip_remind));
        buffer.append("</font>");
        buffer.append("<font color = '#df3051'>");
        buffer.append(remindCount);
        buffer.append("</font>");
        return  Html.fromHtml(buffer.toString());
    }

    /**
     * 设置参与人数样式
     * @param participateCount
     * @return
     */
    private Spanned getParticipateCountStyle(int participateCount){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_participate_prefix));
        buffer.append("</font>");
        buffer.append("<font color = '#3fadf9'>");
        buffer.append(participateCount);
        buffer.append("</font>");
        buffer.append("<font color = '#aaaaaa'>");
        buffer.append(mContext.getString(R.string.ip_records_tip_participate_suffix));
        buffer.append("</font>");
        return Html.fromHtml(buffer.toString());
    }

    @Override
    public void onClick(View view) {
        IPRecordGoingEntity item = (IPRecordGoingEntity) view.getTag();
        if (item == null) {
            return;
        }
        switch (view.getId()){
            //追加
            case R.id.tv_ongoing_item_add:
                Logger.d(item.getTitle());
                break;
            //查看夺宝号
            case R.id.tv_ongoing_item_look_number:
                Logger.d(item.getId());
                break;
        }
    }
}